const {
  getCategoryThemeService,
  getCategoryByThemeIdService,
  getCategoryByRandomService,
  getThemeIncludeCategoryService,
  getCategoryBannersService,
  getActivityClassifyService,
  getActivityByRecommendService,
  releaseActivityService,
  getActivityService,
  complaintServer,
  opinionServer,
} = require("../service/community.service");
const {
  getMissingKeys,
  releaseActivity_Parameter,
  paging_Parameter,
} = require("../config/parameter");
const { PARAMETER_MISSING } = require("../config/error-constants");

class CommunityController {
  async getCategoryBanners(ctx, next) {
    const result = await getCategoryBannersService();

    console.log(result);
    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取社区轮播图成功~`,
      data: result,
    };
  }

  async getCategoryTheme(ctx, next) {
    const result = await getCategoryThemeService();

    console.log(result);
    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取分类主题成功~`,
      data: result,
    };
  }

  async getThemeIncludeCategory(ctx, next) {
    const result = await getThemeIncludeCategoryService();

    console.log(result);
    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取分类主题成功~`,
      data: result,
    };
  }

  async getCategoryByThemeId(ctx, next) {
    const { themeId } = ctx.params;
    // console.log(themeId);
    const result = await getCategoryByThemeIdService(themeId);

    // console.log(result);
    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `获取${themeId}获取圈子成功~`,
      data: result,
    };
  }

  async getCategoryByRandom(ctx, next) {
    const { randomCount } = ctx.query;
    // console.log(randomCount);
    const result = await getCategoryByRandomService(randomCount);

    // console.log(result);
    //4.查看存储的结果，·告知前端创建成
    ctx.body = {
      code: 200,
      message: `随机获取${randomCount}个圈子成功~`,
      data: result,
    };
  }

  async getActivityClassify(ctx, next) {
    // const { randomCount } = ctx.query;
    const result = await getActivityClassifyService();

    console.log("获取活动分类", result);
    ctx.body = {
      code: 200,
      message: `获取活动分类成功~`,
      data: result,
    };
  }

  async getActivityByRecommend(ctx, next) {
    // const { randomCount } = ctx.query;
    const result = await getActivityByRecommendService();

    // console.log("获取活动招募页的推荐活动", result);
    ctx.body = {
      code: 200,
      message: `获取活动招募页的推荐活动成功 ~`,
      data: result,
    };
  }

  /**
   * 发布活动
   * @param {*} ctx
   * @param {*} next
   */
  async releaseActivity(ctx, next) {
    const activityParameter = {};
    activityParameter.uid = ctx.userInfo.id;
    const requestParameter = ctx.request.body;
    Object.assign(activityParameter, requestParameter);

    const verifyParameter = getMissingKeys(
      releaseActivity_Parameter,
      requestParameter
    );
    if (verifyParameter.length) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, verifyParameter);
    }

    const result = await releaseActivityService(activityParameter);

    console.log("发布活动成功", result);
    ctx.body = {
      code: 200,
      message: `发布活动成功 ~`,
      data: result,
    };
  }

  /**
   * 分页获取活动
   * @param {*} ctx
   * @param {*} next
   */
  async getActivity(ctx, next) {
    /* 验证参数 */
    /* 如果不浅拷贝出来，直接写ctx.query，值为[Object: null prototype] { limit: '3' } */
    const pagingParameter = getMissingKeys(paging_Parameter, { ...ctx.query });
    // console.log("pagingParameter", pagingParameter);
    if (pagingParameter.length) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, pagingParameter);
    }

    const { limit, offset } = ctx.query;
    const result = await getActivityService(limit, offset);
    ctx.body = {
      code: 200,
      message: `获取活动数据成功~`,
      data: result,
    };
  }

  /**
   * 举报
   * @param {*} ctx
   * @param {*} next
   * @returns
   */
  async complaint(ctx, next) {
    /* 验证参数 */
    const complaintParameter = {};
    complaintParameter.uid = ctx.userInfo.id;
    const requestParameter = ctx.request.body;

    if (!requestParameter.body) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "body");
    }

    if (!requestParameter.activityid && !requestParameter.articleid) {
      return ctx.app.emit(
        "error",
        PARAMETER_MISSING,
        ctx,
        "activityid / articleid"
      );
    }

    Object.assign(complaintParameter, requestParameter);

    const result = await complaintServer(complaintParameter);
    ctx.body = {
      code: 200,
      message: `举报信息成功，请等待通知~`,
      data: result,
    };
  }

  /**
   * 意见
   * @param {*} ctx
   * @param {*} next
   * @returns
   */
  async opinion(ctx, next) {
    /* 验证参数 */
    const uid = ctx.userInfo.id;
    const { body } = ctx.request.body;

    if (!body) {
      return ctx.app.emit("error", PARAMETER_MISSING, ctx, "body");
    }

    const result = await opinionServer(uid, body);
    ctx.body = {
      code: 200,
      message: `感谢您的建议和意见~`,
      data: result,
    };
  }
}

module.exports = new CommunityController();
